<template>
  <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body :before-close="closeDelDialog">
    <el-form ref="daForm" :model="daForm" :rules="daRules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="批次号" prop="batchNum">
            <el-input v-model="daForm.batchNum" placeholder="请输入批次号" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务名称" prop="taskName">
            <el-input v-model="daForm.taskName" placeholder="请输入任务名称" :disabled="isType=='2'" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务负责人" prop="taskLeader">
            <el-input v-model="daForm.taskLeader" placeholder="请输入任务负责人" :disabled="isType=='2'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务类型" prop="taskType">
            <el-select v-model="daForm.taskType" clearable placeholder="请选择" style="width: 100%" :disabled="isType!='0'">
              <el-option v-for="dict in taskTypeOption" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="daForm.remark" type="textarea" placeholder="请输入备注" ref="saftRef" :disabled="isType=='2'"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick" v-show="isType!='0'">
      <!-- start 按卷上架 -->
      <el-tab-pane label="按卷上架" name="first">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" @click="handleAddDa(1)" :disabled="isType=='2'">添加档案</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" @click="handleAddRemoveDa(1)" :disabled="isType=='2'">移除</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="juanLoading" :data="juanList" @selection-change="juanSelectionChange" height="300">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" type="index" :index="table_index_juan" prop="id" />
          <el-table-column label="案卷题名" prop="title" :show-overflow-tooltip="true" />
          <el-table-column label="案卷号" prop="ajh" :show-overflow-tooltip="true" />
          <el-table-column label="档号" prop="dh" :show-overflow-tooltip="true" />
          <el-table-column label="保管期限">
            <template slot-scope="scope">
              <span v-for="(i, idx) in dict.type.BGQX" :key="idx">
                {{ scope.row.bgqx == i.value ? i.label : '' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="归档司局" prop="gdsj" :show-overflow-tooltip="true" />
          <el-table-column label="归档处室" prop="gdcs" :show-overflow-tooltip="true" />
          <el-table-column label="上架时间" prop="sjTime" :show-overflow-tooltip="true" />
          <el-table-column label="位置" prop="position" :show-overflow-tooltip="true" />
        </el-table>
        <pagination v-show="juanTotal > 0" :total="juanTotal" :page.sync="juanParams.pageNum"
                    :limit.sync="juanParams.pageSize" @pagination="getJuanList" />
      </el-tab-pane>
      <!-- end 按卷上架 -->

      <!-- start 按盒上架 -->
      <el-tab-pane label="按盒上架" name="second">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" @click="handleAddDa(0)" :disabled="isType=='2'">添加档案</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" @click="handleAddRemoveDa(0)" :disabled="isType=='2'">移除</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="heLoading" :data="heList" @selection-change="heSelectionChange" height="300">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" type="index" :index="table_index_he" prop="id" />
          <el-table-column label="盒号" prop="hehao" :show-overflow-tooltip="true" />
          <el-table-column label="档案门类" prop="daml" :show-overflow-tooltip="true" />
          <!--              <el-table-column label="整理方式" prop="zlfs" :show-overflow-tooltip="true" />-->
          <el-table-column label="保管期限">
            <template slot-scope="scope">
              <span v-for="(i, idx) in dict.type.BGQX" :key="idx">
                {{ scope.row.bgqx == i.value ? i.label : '' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="起始年度" prop="boxStnd" :show-overflow-tooltip="true" />
          <el-table-column label="终止年度" prop="boxEdnd" :show-overflow-tooltip="true" />
          <el-table-column label="件号(起)" prop="boxStnum" :show-overflow-tooltip="true" />
          <el-table-column label="件号(止)" prop="boxEdnum" :show-overflow-tooltip="true" />
          <el-table-column label="规格" prop="specName" :show-overflow-tooltip="true" />
          <el-table-column label="上架时间" prop="sjTime" :show-overflow-tooltip="true" />
          <el-table-column label="位置" prop="position" :show-overflow-tooltip="true" />
        </el-table>
        <pagination v-show="heTotal > 0" :total="heTotal" :page.sync="heParams.pageNum"
                    :limit.sync="heParams.pageSize" @pagination="getHeList" />
      </el-tab-pane>
      <!-- end 按盒上架 -->
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-show="isType!='2'">提 交</el-button>
      <el-button  @click="closeDelDialog">取 消</el-button>
    </div>

    <!-- 添加档案 -->
    <AddDa v-if="addDaOpen" :addDaOpen="addDaOpen" @closeDelDialog="closeDelDialogTab" :isShowJuan="isShowJuan" :putawayStatus="putawayStatus" :renwuId="renwuId"></AddDa>
  </el-dialog>
</template>

<script>
import {addDajs, findByDa, updateDajs, updateStatus} from "@/api/archive/warehouseManagement/technicalProcessing";
import AddDa from "@/views/archive/warehouseManagement/technicalProcessing/addDa";

export default {
  name: "gearShifting",
  components: {AddDa},
  // isType: 0->添加;1->调档；2->详情
  props: ['title', 'open', 'rowData', 'isType'],
  archive_Dicts: ['BGQX'],
  data() {
    return {
      // 弹出层表单
      daForm: {
        batchNum: undefined,
        taskName: undefined,
        taskLeader: undefined,
        taskType: undefined,
        remark: undefined
      },
      // 弹出层表单校验
      daRules: {
        batchNum: [
          { required: true, message: "批次号不能为空" }
        ],
        taskName: [
          { required: true, message: "任务名称不能为空" }
        ],
        taskLeader: [
          { required: true, message: "任务负责人不能为空" }
        ],
        taskType: [
          { required: true, message: "任务类型不能为空" }
        ],
      },
      // 任务类型
      taskTypeOption: [
        { value: '3', label: '数字化'},
        { value: '4', label: '缩微'},
        { value: '5', label: '修复'},
        { value: '6', label: '保护技术处理'},
     //   { value: '7', label: '其他情况'},
      ],
      activeName: 'first',
      juanParams: {
        pageNum: 1,
        pageSize: 10,
      },
      juanLoading: false,
      // 按卷上架表格数据
      juanList: [],
      // 总条数
      juanTotal: 0,
      // 选中数据
      juanIds: [],
      // 非单个禁用
      juanSingle: true,
      // 非多个禁用
      juanMultiple: true,

      heParams: {
        pageNum: 1,
        pageSize: 10,
      },
      heLoading: false,
      // 按卷上架表格数据
      heList: [],
      // 总条数
      heTotal: 0,
      // 选中数据
      heIds: [],
      // 非单个禁用
      heSingle: true,
      // 非多个禁用
      heMultiple: true,

      addDaOpen: false,
      isShowJuan: undefined,
      putawayStatus: undefined
    }
  },
  watch: {
    rowData: {
      handler(newVal) {
        // console.log(newVal, '-------------------------')
        this.daForm.batchNum = newVal.batchNum
        if (newVal.id != undefined) {
          this.daForm = newVal
          this.juanParams.id = newVal.id
          this.juanParams.sjType = '1'
     //     this.juanParams.taskType = newVal.taskType + '0'
          this.getJuanList()

        }
        console.log(this.dataForm,"---")
      },
      immediate:true
    }
  },
  methods: {
    submitForm() {
      this.$refs["daForm"].validate(valid => {
        if (valid) {
          if (this.daForm.id != undefined) {
            updateDajs(this.daForm).then(res=>{
              this.$modal.msgSuccess('调档成功')
              this.$emit('submitForm')
            })
          } else {
            addDajs(this.daForm).then(res=>{
              this.$modal.msgSuccess('添加成功')
              this.$emit('submitForm')
            })
          }
        }
      })
    },
    // 弹窗取消
    closeDelDialog() {
      this.$emit('closeDelDialog')
      this.reset()
      this.resetForm('daForm')
    },
    reset() {
      this.daForm = {
        batchNum: undefined,
        taskName: undefined,
        taskLeader: undefined,
        taskType: undefined,
        remark: undefined
      }
    },
    handleClick(tab, event) {
      if (tab.name=='first') {
        this.getJuanList()
      } else {
        this.getHeList()
      }
    },

    // 添加档案
    handleAddDa(val) {
      if (val == 1) {
        this.isShowJuan = true
      } else {
        this.isShowJuan = false
      }
      this.putawayStatus = this.rowData.taskType
      this.renwuId = this.rowData.id
      this.addDaOpen = true
      console.log(this.addDaOpen)
    },
    closeDelDialogTab() {
      this.addDaOpen = false
      this.getJuanList()
      this.getHeList()
    },
    // 移除
    handleAddRemoveDa(val) {
      var form = {
        status: this.rowData.taskType,
        type: '2',
        id: this.rowData.id
      }
      if (val == 1) {
        form.ids = this.juanIds
      } else {
        form.ids = this.heIds
      }
      if (form.ids.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据')
        return
      }
      updateStatus(form).then(res=>{
        this.$modal.msgSuccess('移除成功')
        this.getJuanList()
        this.getHeList()
      })
    },

    // 按卷上架
    juanSelectionChange(selection) {
      this.juanIds = selection.map(item => item.boxId)
      this.juanSingle = selection.length != 1
      this.juanMultiple = !selection.length
    },
    table_index_juan(index) {
      return (this.juanParams.pageNum - 1) * this.juanParams.pageSize + index + 1;
    },
    getJuanList() {
      this.juanLoading = true
      this.juanParams.id = this.rowData.id
      this.juanParams.sjType = '1'
      // this.juanParams.taskType = this.rowData.taskType + '0'
      findByDa(this.juanParams).then(res=>{
        this.juanLoading = false
        this.juanList = res.rows
        this.juanTotal = res.total
      })
    },

    // 按盒上架
    heSelectionChange(selection) {
      this.heIds = selection.map(item => item.boxId)
      this.heSingle = selection.length != 1
      this.heMultiple = !selection.length
    },
    table_index_he(index) {
      return (this.heParams.pageNum - 1) * this.heParams.pageSize + index + 1;
    },
    getHeList() {
      this.heLoading = true
      this.heParams.id = this.rowData.id
      this.heParams.sjType = '0'
      // this.heParams.taskType = this.rowData.taskType + '0'
      findByDa(this.heParams).then(res=>{
        this.heLoading = false
        this.heList = res.rows
        this.heTotal = res.total
      })
    }
  }
}
</script>

<style  lang="scss" scoped>

</style>
